﻿using System;
using System.Collections.Generic;
using System.Text;
using Pixysoft.Framework.Reflection;
using Pixysoft.Weblications.Sango.Protocol.player;
using Pixysoft.Weblications.Sango.Protocol.equipment;
using Pixysoft.Weblications.Sango.Dao;
using Pixysoft.Weblications.Sango.Formular;
using Pixysoft.Weblications.Sango.Constant;
using Pixysoft.Weblications.Sango.Protocol;

namespace Pixysoft.Weblications.Sango.Command.equipment
{
    partial class SangoEquipmentHandler
    {
        public eqm_upgrade eqm_upgrade(string primarycode)
        {
            string userCode = SangoManager.Authentication();

            eqm_upgrade response = ReflectionManager.CreatePojo<eqm_upgrade>();
            response.Succeed = false;

            Entities.Usr_Profile profile = UsrProfileDao.getUsrProfile(userCode);


            // 获取成功率相关

            long succeedrate = EquipmentDao.getSuccessRate(profile.Userlevel);

            bool trend = EquipmentFormular.getSucceedTrend();

            // 获取装备

            Entities.Usr_Equipment eqm = EquipmentDao.tryGetUserNotBufferFragmentEqm(primarycode);

            if (eqm == null)
            {
                response.Message = "装备不存在";
                return response;
            }

            Entities.Usr_Building building = UsrBuildingDao.getUserBuilding(userCode, CtiBuildingTypecodeCollection.SHOP);

            // 装备冷却相关

            int timespan = EquipmentFormular.default_queue_cooltimespan;

            Entities.Usr_Queue queue = UsrQueueDao.UsrQueueStartup(userCode, CtiQueueTypecodeCollection.EQUIPMENT_UPGRADE, userCode, timespan);

            if (queue == null)
            {
                response.Message = "队列冷却中";
                return response;
            }

            if (eqm.Gradelevel >= building.Buildinglevel)
            {
                response.Message = "装备等级无法超越建筑等级";
                return response;
            }

            // 现金扣除

            long cashprice = EquipmentFormular.getUpgradePrice(eqm);

            if (!UsrProfileDao.tryDecreaseCashvalue(profile, cashprice))
            {
                response.Message = "银币不足";
                return response;
            }

            Entities.Usr_Mission mission = MissionDao.getMission(userCode);

            // 数据库操作

            bool upgradeResult = false;

            IEntityNoebeTransaction command = SettingHelper.EntityTransaction;

            command.Begin(delegate()
            {
                // 现金升级

                upgradeResult = EquipmentFormular.upgrade(eqm, succeedrate);

                command.Update(profile);

                command.Update(eqm);

                command.Update(queue);

                if (MissionDao.processDailyMission(mission, MissionDailyCollection.EQM_UPGRADE))
                {
                    command.Update(mission);
                }

                command.Commit();
            });


            response.successrate = succeedrate;
            response.trend = trend;
            response.upgraderesult = upgradeResult;

            response.profile = UsrProfileFacade.ToProfile(profile);
            response.queue = UsrQueueFacade.ToQueue(queue);
            response.usereqm = EquipmentFacade.ToEquipment(eqm, EquipmentDao.getUserEqmExtras(userCode, eqm.Primarycode));

            response.Succeed = true;
            return response;
        }


    }
    public interface eqm_upgrade : ISangoResponse
    {
        player_profile profile { get;set;}
        player_queue queue { get;set;}
        usr_equipment usereqm { get;set;}
        long successrate { get;set;}
        bool trend { get;set;}
        bool upgraderesult { get;set;}
    }
}
